<?php
namespace Common\Models;
use Common\BaseModel;
class CategoryHaodanku extends BaseModel
{
    public $res;
    public $redis_key = "index_category";

    public function initialize()
    {
        parent::initialize();
        $this->setSource('f_categories_haodanku');
    }



    //获取所有分类
    public function get_find($cid=0)
    {
        if($cid==0){
            $conditions="parent_id=$cid";
            $res = $this->find(
                array(
                    $conditions,
                    'columns'=>'name,value as category_id,site,image',
                )
            )->toarray();
            return $res;
        }else{
            $conditions="value=$cid and parent_id=0";
            $first_find = $this->findFirst(array(
                $conditions,
            ));
            if($first_find){
                $res = $this->find(
                    array(
                        "parent_id={$first_find->id}",
                        'columns'=>'id,name,value as category_id,site,image',
                    )
                )->toarray();
                if($res){
                    foreach($res as $key=>$value){
                        $res[$key]['child'] = $this->find(
                            array(
                                "parent_id={$value['id']}",
                                'columns'=>'name,value as category_id,site,image',
                            )
                        )->toarray();
                    }
                    return $res;
                }else{
                    return [];
                }
            }else{
                return [];
            }
        }
    }





    //获取所有分类
    public function get_find_index($cid=0)
    {
        $conditions="value=$cid and image<>''";
        $res = $this->find(
            array(
                $conditions,
                'columns'=>'id,name,value as category_id,site,image',
                'limit'=>10
            )
        )->toarray();
        return $res;
    }


}
